document.addEventListener('DOMContentLoaded', function() {
    // 获取DOM元素
    const startBtn = document.getElementById('start-btn');
    const pauseBtn = document.getElementById('pause-btn');
    const stopBtn = document.getElementById('stop-btn');
    const sleepTimeDisplay = document.getElementById('sleep-time-display');
    const eegGraph = document.getElementById('eeg-graph');
    
    // 状态变量
    let isMonitoring = false;
    let isPaused = false;
    let seconds = 0;
    let timerInterval;
    let graphInterval;
    
    // 绘图上下文
    const ctx = eegGraph.getContext('2d');
    
    // 脑电数据数组
    let eegData = Array(600).fill(0);
    
    // 初始化绘图区域
    function initGraph() {
        ctx.fillStyle = '#f5f5f5';
        ctx.fillRect(0, 0, eegGraph.width, eegGraph.height);
        
        // 绘制网格线
        ctx.strokeStyle = '#ddd';
        ctx.lineWidth = 0.5;
        
        // 横线
        for (let y = 0; y < eegGraph.height; y += 40) {
            ctx.beginPath();
            ctx.moveTo(0, y);
            ctx.lineTo(eegGraph.width, y);
            ctx.stroke();
        }
        
        // 竖线
        for (let x = 0; x < eegGraph.width; x += 40) {
            ctx.beginPath();
            ctx.moveTo(x, 0);
            ctx.lineTo(x, eegGraph.height);
            ctx.stroke();
        }
    }
    
    // 更新脑电图
    function updateGraph() {
        // 清除画布
        ctx.fillStyle = '#f5f5f5';
        ctx.fillRect(0, 0, eegGraph.width, eegGraph.height);
        
        // 绘制网格线
        initGraph();
        
        // 生成随机数据（模拟脑电信号）
        const newValue = Math.random() * 100 - 50; // -50 到 50 之间的随机值
        eegData.push(newValue);
        eegData.shift(); // 移除最旧的数据点
        
        // 绘制脑电线
        ctx.strokeStyle = '#8146ff';
        ctx.lineWidth = 2;
        ctx.beginPath();
        
        for (let i = 0; i < eegData.length; i++) {
            // 将数据范围从 -50~50 映射到画布高度
            const y = eegGraph.height / 2 - eegData[i] * (eegGraph.height / 200);
            
            if (i === 0) {
                ctx.moveTo(i, y);
            } else {
                ctx.lineTo(i, y);
            }
        }
        
        ctx.stroke();
    }
    
    // 更新睡眠时间显示
    function updateTimer() {
        seconds++;
        const hours = Math.floor(seconds / 3600);
        const minutes = Math.floor((seconds % 3600) / 60);
        const secs = seconds % 60;
        
        sleepTimeDisplay.textContent = 
            `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
    }
    
    // 开始监测
    startBtn.addEventListener('click', function() {
        if (!isMonitoring) {
            isMonitoring = true;
            isPaused = false;
            
            // 开始计时
            timerInterval = setInterval(updateTimer, 1000);
            
            // 开始绘制脑电图
            initGraph();
            graphInterval = setInterval(updateGraph, 50);
            
            this.disabled = true;
            pauseBtn.disabled = false;
            stopBtn.disabled = false;
        }
    });
    
    // 暂停监测
    pauseBtn.addEventListener('click', function() {
        if (isMonitoring && !isPaused) {
            isPaused = true;
            clearInterval(timerInterval);
            clearInterval(graphInterval);
            
            this.textContent = '继续';
        } else if (isMonitoring && isPaused) {
            isPaused = false;
            timerInterval = setInterval(updateTimer, 1000);
            graphInterval = setInterval(updateGraph, 50);
            
            this.textContent = '暂停';
        }
    });
    
    // 结束监测
    stopBtn.addEventListener('click', function() {
        if (isMonitoring) {
            isMonitoring = false;
            isPaused = false;
            
            clearInterval(timerInterval);
            clearInterval(graphInterval);
            
            // 重置计时器和图表
            seconds = 0;
            sleepTimeDisplay.textContent = '00:00:00';
            eegData = Array(600).fill(0);
            initGraph();
            
            startBtn.disabled = false;
            pauseBtn.disabled = true;
            stopBtn.disabled = true;
            pauseBtn.textContent = '暂停';
        }
    });
    
    // 初始化
    pauseBtn.disabled = true;
    stopBtn.disabled = true;
    initGraph();
}); 